#include <cstdio>
#include <cassert>

typedef int pixel_t[3];

static inline void write_ppm(const int resx, const int resy, const pixel_t image[], const char filename[])
{
    // image is assumed stored in rwo-major order
    FILE * const f = std::fopen(filename, "w");
    assert(f);
    std::fprintf(f, "P3 %d %d 255\n", resx, resy);
    for (int j = 0; j < resy; ++j)
    for (int i = 0; i < resx; ++i)
    {
        std::fprintf(f, "%d %d %d\n", image[i + j*resx][0], image[i + j*resx][1], image[i + j*resx][2]);
    }
    std::fclose(f);
}




static inline void read_ppm(int& resx, int& resy, pixel_t * image, const char filename[])
{
    FILE * const f = std::fopen(filename, "r");
    assert(f);
    std::fscanf(f, "P3 %d %d 255\n", &resx, &resy);
    image = new pixel_t[resx*resy];
    for (int j = 0; j < resy; ++j)
    for (int i = 0; i < resx; ++i)
    {
        std::fscanf(f, "%d %d %d", &image[i + j*resx][0], &image[i + j*resx][1], &image[i + j*resx][2]);
    }
    std::fclose(f);
}



